package com.example.attendancesystem.Mapper;


import com.example.attendancesystem.Models.Employee;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface EmployeesMapper {

    /**
     * 查询所有员工信息 部分信息 用于缺勤记录
     *
     * @return 员工对象列表
     */
    @Select("select emp_id,emp_number,emp_name,emp_tel,emp_email,address,emp_position,department from employees")
    List<Employee> getAllEmpUseAbsence();

    /**
     * 通过员工工号查询员工信息 要查密码 用于添加员工和登录
     *
     * @param empNumber 员工工号
     * @return 员工对象
     */
    @Select("select emp_id,emp_number,emp_password,emp_name,emp_gender,add_time,ques,ans," +
            "emp_tel,emp_email,address,img_path,education,emp_position,department,birthday " +
            "from employees where emp_number = #{empNumber}")
    List<Employee> getEmpByEmpNumber(@Param("empNumber") String empNumber);

    /**
     * 查询员工总数
     *
     * @return 员工总记录条数
     */
    @Select("select COUNT(emp_id) from employees")
    Integer getEmpSum();

    /**
     * 查询员工工号出现次数
     *
     * @return 员工总记录条数
     */
    @Select("select COUNT(emp_number) from employees where emp_number = #{addEmpNum} ")
    Integer getEmpNumberSum(@Param("addEmpNum") String addEmpNum);

    /**
     * 分页查询所有员工信息
     *
     * @param offset 页码
     * @param limit  每页限制条数
     * @return List<Employee>对象
     */
    @Select("select emp_id,emp_number,emp_name,emp_gender,add_time," +
            "emp_tel,emp_email,address,img_path,education,emp_position,department,birthday " +
            "from employees limit #{offset},#{limit}")
    List<Employee> getAllEmp(@Param("offset") Integer offset, @Param("limit") Integer limit);


    @Select("select emp_id,emp_number,emp_name,emp_gender,add_time," +
            "emp_tel,emp_email,address,img_path,education,emp_position,department,birthday " +
            "from employees")
    List<Employee> getAllEmpDown();

    /**
     * 根据工号删除员工信息
     *
     * @param empNumber 员工工号
     * @return Integer对象 执行操作受到影响的条数
     */
    @Delete("delete from employees where  emp_number = #{empNumber}")
    Integer delEmpByNumber(@Param("empNumber") String empNumber);

    /**
     * 按员工号或者姓名查询
     *
     * @param emp 员工号 姓名
     * @return List<Employee>对象
     */
    @Select("select emp_id,emp_number,emp_name,emp_gender,add_time," +
            "emp_tel,emp_email,address,img_path,education,emp_position,department,birthday " +
            "from employees where emp_number like #{emp} or emp_name like #{emp}")
    List<Employee> queryEmpByNameOrNumber(@Param("emp") String emp);

    /**
     * 新增员工
     *
     * @param addEmp 新员工对象
     * @return 操作受到影响的记录条数
     */
    @Insert("insert into employees  " +
            "(emp_number,emp_password,emp_name,emp_gender,add_time,ques,ans,emp_tel,emp_email,address,img_path,education,emp_position,department,birthday) " +
            "values(#{addEmp.empNumber},#{addEmp.empPassword},#{addEmp.empName},#{addEmp.empGender},#{addEmp.addTime},#{addEmp.ques},#{addEmp.ans},#{addEmp.empTel}" +
            ",#{addEmp.empEmail},#{addEmp.address},#{addEmp.imgPath},#{addEmp.education},#{addEmp.empPosition},#{addEmp.department},#{addEmp.birthday})")
    Integer addEmp(@Param("addEmp") Employee addEmp);

    /**
     * 更新员工密码、密保、密保答案
     *
     * @param empPass   员工密码
     * @param empQues   密保问题
     * @param empAns    密保答案
     * @param empNumber 要更新的员工号
     * @return 操作受到影响的记录条数
     */
    @Update("update employees set emp_password = #{empPass},ques = #{empQues}, ans = #{empAns} where emp_number = #{empNumber} ")
    Integer updateEmpPass(@Param("empPass") String empPass, @Param("empQues") String empQues, @Param("empAns") String empAns, @Param("empNumber") String empNumber);

    /**
     * 更新员工密码
     *
     * @param empNumber 员工号
     * @param newPass   密码
     * @return 操作受到影响的记录条数
     */
    @Update("update employees set emp_password = #{newPass} where emp_number = #{empNumber} ")
    Integer updateEmpPassByEmpNum(@Param("empNumber") String empNumber, @Param("newPass") String newPass);


    /**
     * 更新员工的头像
     *
     * @param empNumber 员工号
     * @param imgPath   新的图片名称
     * @return 操作受到影响的记录条数
     */
    @Update("update employees set img_path = #{imgPath} where emp_number = #{empNumber}")
    Integer updateEmpImgPath(@Param("empNumber") String empNumber, @Param("imgPath") String imgPath);


    /**
     * 修改员工信息
     *
     * @param employee     要修改员工
     * @param oldEmpNumber 原来的员工号
     * @return 操作受到影响的记录条数
     */
    @Update("update employees set " +
            "emp_number = #{employee.empNumber},emp_tel = #{employee.empTel},emp_email = #{employee.empEmail} ," +
            " education = #{employee.education} ,emp_position = #{employee.empPosition},department =#{employee.department} " +
            "where emp_number = #{oldEmpNumber}")
    int updateEmpInfo(@Param("employee") Employee employee, @Param("oldEmpNumber") String oldEmpNumber);
}
